package com.ocom.exposeapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.entity.card.EntityXfMerchPayInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.RequestUtil;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.SignUtils;
import com.ocom.exposeapi.feign.CompanyClientService;
import com.ocom.exposeapi.feign.PayConfigClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2022/8/17
 **/
@Slf4j
@RestController
@RequestMapping("/ocom_pay")
public class PayController {

	String url = "http://192.168.3.123:32880/ocom_pay/";

	@Autowired
	CompanyClientService companyClientService;

	@Autowired
	PayConfigClientService payConfigClientService;

	@PostMapping("/{path1}/{path2}")
	public Object daiKou(@PathVariable("path1") String path1,@PathVariable("path2") String path2, @RequestBody Map<String,Object> map){

		try {
			SortedMap<String, Object> parameters = new TreeMap<String, Object>();
			for (Map.Entry<String, Object> entry : map.entrySet()) {
				String key = entry.getKey().toLowerCase();
				parameters.put(key, entry.getValue());
			}

			if(!map.containsKey("merchId")){
				return ResultUtil.info(201,"商户Id为空");
			}

			if(!map.containsKey("comId")){
				return ResultUtil.info(201,"单位Id为空");
			}

			Result result = companyClientService.getCompanyKeyInfoOne(((Integer) map.get("comId")).longValue());
			if (result.getCode() != 200) {
				log.info("支付请求-异常 ，路由【{}】  参数【{}】  异常【{}】",path1+"/"+path2,map,"获取密钥失败");
				return ResultUtil.info(204,"单位密钥未配置");
			}
			JSONObject jsonObject= JSON.parseObject(JSON.toJSONString(result.getData()));
			if(!jsonObject.containsKey("commKey")  || CommonUtil.isNull(jsonObject.getString("commKey"))){
				log.info("支付请求-异常-没有密钥 ，路由【{}】  参数【{}】  异常【{}】",path1+"/"+path2,map,jsonObject);
				return ResultUtil.info(204,"单位密钥未配置");
			}

			String commKey=jsonObject.getString("commKey");

			String realSign = SignUtils.createSign(parameters, commKey);
			String sign =(String)  map.get("sign");

			if(CommonUtil.isNull(sign)){
				return ResultUtil.info(203,"sign为空");
			}
			if(!sign.equals(realSign)){
				log.info("支付请求-异常 ，路由【{}】  参数【{}】 【{}】 【{}】 异常【{}】",path1+"/"+path2,map,sign,realSign,"验签失败");
				return ResultUtil.error(ResultEnum.Err_202);
			}


			Integer merchId= (Integer) map.get("merchId");

			EntityXfMerchPayInfo entityXfMerchPayInfo=new EntityXfMerchPayInfo();
			entityXfMerchPayInfo.setComId(((Integer) map.get("comId")).longValue());
			entityXfMerchPayInfo.setMerchId(merchId);

			Result result1 = payConfigClientService.getPayInfo(entityXfMerchPayInfo);
			if(result1.getCode()!=200){
				return ResultUtil.info(205,"单位未配置支付通道");
			}

			EntityXfMerchPayInfo entityXfMerchPayInfo1 = JSON.parseObject(JSON.toJSONString(result1.getData()),EntityXfMerchPayInfo.class);
			map.put("company_id",entityXfMerchPayInfo1.getCompanyId());
			map.put("user_name",entityXfMerchPayInfo1.getUserName());
			map.put("password",entityXfMerchPayInfo1.getPassword());

			String  RealUrl   = url+ path1 +"/"+ path2;
			RequestUtil requestUtil = new RequestUtil();
			Map<String, String> re  = requestUtil.doPostJson(RealUrl, JSON.toJSONString(map));
			log.info("支付请求-返回 ，路由【{}】  参数【{}】 异常【{}】",path1+"/"+path2,map,re);
			return re;

		}catch (Exception e){
			log.info("支付请求异常 ，路由【{}】  参数【{}】 异常【{}】",path1+"/"+path2,map,e);
			return ResultUtil.info(206,"系统内部异常，请联系管理员");
		}

	}
}
